﻿/*global analogue*//*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 4*/

analogue.elements = (function (window) {
    var booleans = {
        'checked': 'checked',
        'disabled': 'disabled',
        'multiple': 'multiple',
        'readonly': 'readOnly',
        'selected': 'selected'
    }, detect = analogue.detect, getOpacity = function (element) {
        if (detect.opacity) {
            return window.getComputedStyle(element, null).opacity || 1;
        }
        try {
            return element.filters.item('DXImageTransform.Microsoft.Alpha').opacity / 100;
        } catch (e) {
            try {
                return element.filters.alpha.opacity / 100;
            } catch (er) {}
        }
        return 1;
    }, i, properties = {
        'accesskey': 'accessKey',
        'class': 'className',
        'enctype': 'encType',
        'float': detect.cssFloat,
        'for': 'htmlFor',
        'html': 'innerHTML',
        'maxlength': 'maxLength',
        'tabindex': 'tabIndex',
        'text': detect.textContent
    };
    for (i in booleans) {
        if (booleans.hasOwnProperty(i)) {
            properties[i] = booleans[i];
        }
    }
    return {
        addClass: function (value) {
            var classes = this.className.split(' ');
            if (!this.hasClass(value)) {
                classes.push(value);
                this.className = classes.join(' ');
            }
            return this;
        },
        getProperty: function (attribute) {
            if (attribute === 'style' && !detect.style) {
                return this.style.cssText.toLowerCase();
            }
            if (attribute === 'opacity') {
                return getOpacity(this);
            }
            if ((/^(href|src|type)$/i).test(attribute) && !detect.href) {
                return this.getAttribute(attribute, 2);
            }
            if (booleans[attribute]) {
                return !!this[properties[attribute]];
            }
            return properties[attribute] ? this[properties[attribute]] : this.getAttribute(attribute);
        },
        hasClass: function (value) {
            return this.className && new RegExp('(^|\\s)' + value + '(\\s|$)').test(this.className);
        },
        removeClass: function (value) {
            var classes = this.className.split(' '), i, ii;
            for (i = 0, ii = classes.length; i < ii; i += 1) {
                if (classes[i] === value) {
                    classes.splice(i, 1);
                }
            }
            this.className = classes.join(' ');
            return this;
        },
        setProperty: function (attribute, value) {
            if (attribute === 'style' && !detect.style) {
                this.style.cssText = value;
            } else if (attribute === 'opacity') {
                if (detect.opacity) {
                    this.style.opacity = value;
                } else if (this.style.filter !== undefined) {
                    this.style.filter = 'alpha(opacity=' + value * 100 + ')';
                }
            } else if (attribute.match(/^on(\w+)$/i) && typeof value === 'function') {
                this.addEvent(RegExp.$1, value);
            } else if (properties[attribute]) {
                if (booleans[attribute] && typeof value !== 'boolean') {
                    value = (typeof value !== 'undefined') ? true : false;
                }
                this[properties[attribute]] = value;
            } else {
                this.setAttribute(attribute, value);
            }
            return this;
        },
        toggleClass: function (value) {
            if (this.hasClass(value)) {
                this.removeClass(value);
            } else {
                this.addClass(value);
            }
            return this;
        }
    };
}(this));